package com.sneaker.shower.socket.repository;

import com.sneaker.shower.socket.Group;
import com.sneaker.shower.socket.feature.SignAble;

import java.util.function.Consumer;

/**
 * <h1>GroupRepository</h1>
 * <pre>
 * @Description 组仓库
 * </pre>
 *
 * @Author lopo
 * @Date 2018/10/17
 */
public class GroupRepository<T extends SignAble> extends ListRepository<Group<T>> {


    /**
     * 保存组时保存元素
     * @param channel
     */
    @Override
    public Group<T> save(Group<T> channel) {
        Group<T> group = get(channel);
        if(group == null){
            group = channel;
            super.save(channel);
        }else {
            Group<T> s = group;
            itemOperate(channel,(e)->s.add(e));
        }
        return group;
    }

    private void itemOperate(Group<T> g,Consumer<? super T> action){
        if(g == null){
            return;
        }
        g.group().stream().forEach(action);
    }
}
